package com.ophiux.cs.infrastructure.remote.chat;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.MessagingException;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;

import com.ophiux.cs.domain.chat.model.valueobj.PushCSChatValueObj;
import com.ophiux.cs.domain.chat.model.valueobj.PushChatMessageResult;
import com.ophiux.cs.domain.chat.remote.IPushChatMessageRemoteService;
import com.ophiux.utils.exception.OphiuxRemoteException;

/**
 * 推送websocket即时消息
 * @author chenh
 *
 */
@Service("pushChatMessageRemoteService")
public class PushChatMessageRemoteService implements IPushChatMessageRemoteService {

	@Autowired
	private SimpMessagingTemplate simpMessagingTemplate;
	
	@Override
	public PushChatMessageResult push(PushCSChatValueObj push) throws OphiuxRemoteException {
		//消息实时推送
		try {
			String toUser = push.getToUser();
			String content = push.getContent();
			String fromUser = push.getFromUser();
			
			simpMessagingTemplate.convertAndSendToUser(toUser, 
	                "/message/"+fromUser, content);
		} catch (MessagingException e) {
			return PushChatMessageResult.fail("", e.getFailedMessage().getPayload()+"");
		}
		
		return PushChatMessageResult.success("消息推送成功", "");
	}

}
